package com.mf.generator.service.basic.impl;

import com.mf.generator.domain.basic.mapping.DataTypeMapping;
import com.mf.generator.service.basic.DataTypeMappingService;
import com.mf.generator.utils.Dom4jUtils;
import org.dom4j.Document;
import org.dom4j.Node;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据类型映射Service接口类
 * User: zhaoming
 * Date: 2016-07-02
 * To change this template use File | Settings | File Templates.
 **/
@Service
public class DataTypeMappingServiceImpl implements DataTypeMappingService {

    /**
     * 查询全部数据类型映射列表
     * @return 数据类型映射列表
     */
    public List<DataTypeMapping> selectAll(){
        List<DataTypeMapping> dataTypeMappingList = new ArrayList<>();

        String fileName = this.getClass().getResource("/").toString();
        fileName = fileName.substring("file:/".length(), fileName.length()) + File.separator + "data" + File.separator + "xml" + File.separator + "data-type-mapping.xml";
        Dom4jUtils dom4jUtil = new Dom4jUtils();
        Document document =  dom4jUtil.read(fileName);
        List<Node> list = dom4jUtil.selectNodes(document,"//dataType");
        for(Node node : list){
            DataTypeMapping dataType = new DataTypeMapping();
            dataType.setDatabaseType(node.valueOf("@databaseType"));
            dataType.setJdbcType(node.valueOf("@jdbcType"));
            dataType.setJavaType(node.valueOf("@javaType"));
            dataTypeMappingList.add(dataType);
        }
        return dataTypeMappingList;
    }

    /**
     * 数据类型映射更新
     * @param dataTypeMapping 数据类型映射实体
     * @return 执行结果X  f
     */
    public boolean update(DataTypeMapping dataTypeMapping){
        boolean flag = true;
        try
        {
            String fileName = this.getClass().getResource("/").toString();
            fileName = fileName.substring("file:/".length(), fileName.length()) + File.separator + "data" + File.separator + "xml" + File.separator + "data-type-mapping.xml";

            StringBuffer buffer = new StringBuffer();
            buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            buffer.append("<dataTypes>");
            for(DataTypeMapping data : this.selectAll()){
                if(data.getDatabaseType().equals(dataTypeMapping.getDatabaseType())){
                    buffer.append("<dataType databaseType=\""+dataTypeMapping.getDatabaseType()+"\" jdbcType=\""+dataTypeMapping.getJdbcType()+"\" javaType=\""+dataTypeMapping.getJavaType()+"\"></dataType>");
                }else{
                    buffer.append("<dataType databaseType=\""+data.getDatabaseType()+"\" jdbcType=\""+data.getJdbcType()+"\" javaType=\""+data.getJavaType()+"\"></dataType>");
                }
            }
            buffer.append("</dataTypes>");

            Dom4jUtils dom4jUtil = new Dom4jUtils();
            dom4jUtil.createXMLFile(fileName, buffer.toString(), "prettyPrint");
        }catch(Exception e){
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }
}
